package com.sx.micowell.module.member

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.ContainerActivity
import com.example.mvvm.base.action.TitleBarAction
import com.example.mvvm.base.data.DataBindingConfig
import com.gyf.immersionbar.ImmersionBar
import com.example.mvvm.bar.TitleBar
import com.miloyu.modelnet.response.LearnedCourseVO
import com.miloyu.modelnet.response.MemberSetMealVO
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragPaymentListBinding
import com.sx.micowell.framework.app.AppConstant
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.MemberModel
import com.sx.micowell.framework.model.PaymentModel
import com.sx.micowell.module.learned.LearnCourseListAdapter
import com.sx.micowell.wxapi.WxUtils
import org.json.JSONObject
import java.lang.Exception

/**
 * @author: YY
 * @time: 2021/8/9
 * @describe 支付列表
 */
@Route(path = RoutePath.PAYMENT_LIST)
class PaymentListFragment : MyFragment<FragPaymentListBinding, ContainerActivity>(), TitleBarAction {

    @Autowired(name = IntentKey.DATA)
    @JvmField
    var memberSetMeal: MemberSetMealVO? = null

    var isWx: Boolean = false

    lateinit var viewModel: PaymentModel

    private var paymentListAdapter : PaymentListAdapter? = null


    override fun initViewModel() {
        viewModel = getFragmentScopeViewModel(PaymentModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
       return DataBindingConfig(R.layout.frag_payment_list, BR.viewModel, viewModel)
    }

    override fun initView() {
        // 设置状态栏字体颜色
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()
        binding.title.setOnTitleBarListener(this)
    }

    @SuppressLint("SetTextI18n")
    override fun initData() {
        if (memberSetMeal != null) {
            viewModel.setMealId = memberSetMeal!!.id
            binding.tvPayAmount.text = "¥ ${memberSetMeal!!.money}"
            binding.tvPayAmountTips.text = "开通会员-${memberSetMeal!!.name}"
        }
        viewModel.reqData(AppConstant.DEVICE_TYPE)
    }

    override fun initViewObservable() {
        super.initViewObservable()
        setPayModeList()

        viewModel.openMemberDataEvent.observe(this, {
            var payBody=it
            if (AppConstant.DEVICE_TYPE) {
                // App
                if (isWx) {
                    val wxUtils = WxUtils();
                    wxUtils.pay(it)
                } else {
                    val bundle =  Bundle()
                    bundle.putString(IntentKey.DATA, payBody)
                    startContainerActivity(
                        RouteManager.getInstance().getOpenMemberFragment(payBody).javaClass.canonicalName,
                        bundle)
                }
            } else{
                //Web
                if (isWx) {
                    val bundle = Bundle();
                    bundle.putString(IntentKey.DATA, payBody)
                    startContainerActivity(
                        RouteManager.getInstance().getPayQrCodeFragment(payBody).javaClass.canonicalName,
                        bundle
                    )
                } else {
                    val bundle =  Bundle()
                    bundle.putString(IntentKey.DATA, payBody)
                    startContainerActivity(
                        RouteManager.getInstance().getOpenMemberFragment(payBody).javaClass.canonicalName,
                        bundle)
                }
            }
        })
    }

    override fun getTitleBar(): TitleBar {
       return binding.title
    }

    override fun onLeftClick(v: View?) {
        myActivity.finish()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setPayModeList() {
        //取消recyclerview单独的滑动效果
        binding.recyclerPayment.isNestedScrollingEnabled = false
        //设置布局管理器
        binding.recyclerPayment.layoutManager = LinearLayoutManager(context)

        viewModel.paymentListData.observe(this) { data ->
            paymentListAdapter = PaymentListAdapter()
            paymentListAdapter?.data = data
            binding.recyclerPayment.adapter = paymentListAdapter

            viewModel.payId = data[0].id
            isWx = data[0].name == "微信"

            paymentListAdapter?.setOnItemClickListener { adapter, view, position ->
                viewModel.payId = data[position].id
                isWx = data[position].name == "微信"
                paymentListAdapter?.clickId = position
                adapter.notifyDataSetChanged()
            }
        }
    }


    override fun onResume() {
        super.onResume()
        if (viewModel!=null) {
            viewModel.userInfo()
        }

    }
}